package service;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionContext;

import dao.TutorDao;
import dao.UserDao;
import po.Employment;
import po.Order;
import po.User;



//用户的登陆和注册业务逻辑
public class UserService {
	private int id = 0;
	private Map<String, Object> request, session;
	//登陆逻辑
	public int login(User loginUser) {
		String account = loginUser.getAccount();
		String password = loginUser.getPassword();
		String hql = "from User as user where account='"
							+account+ "' and password='" + password +"'";
		UserDao dao = new UserDao();
		List list = dao.findByHql(hql);
		if(list.isEmpty()) {
			return id;
		}else {
			try {
				Object obj = list.get(0);
				Field f = obj.getClass().getDeclaredField("customerId");
				f.setAccessible(true);
				Object value = f.get(obj);
				id = (int) value;
				System.out.println("用户的id:"+value);
			}catch(Exception e) {
				System.out.println("用户id获取时出现严重问题");
			}
			
			return id;
		}
			
	}
	//
	public boolean isTutor(int id) {
		UserDao dao = new UserDao();
		User user = dao.getUser(id);
		if(user.getIsTutor()==null) {
			return false;
		}
		if(user.getIsTutor().equals("1")) {
			return true;
		}else {
			return false;
		}
	}
	//注册逻辑
	public boolean register(User user) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		UserDao dao = new UserDao();
		Transaction tran = null;
		try {
			tran = dao.getSession().beginTransaction();
			dao.register(user);
			tran.commit();
			request.put("tip", "注册成功！");
			return true;
		} catch (Exception e) {
			if(tran != null) tran.rollback();
			return false;
		} finally {
			dao.getSession().close();
		}
	}
	
	//用户发布招聘信息
	public boolean issue(Employment emp) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		UserDao dao = new UserDao();
		Transaction tran = null;
		try {
			tran = dao.getSession().beginTransaction();
			dao.issue(emp);
			tran.commit();
			request.put("tip", "招聘信息已成功发布！");
			return true;
		} catch (Exception e) {
			if(tran != null) tran.rollback();
			return false;
		} finally {
			dao.getSession().close();
		}
	}
	
	public boolean registerTutor(User user,int id,String path) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		UserDao dao = new UserDao();
		User old = dao.getUser(id);
	
		if(old.getIsTutor()==null) {
			old.getUserDetail().setName(user.getUserDetail().getName());
			
			old.getUserDetail().setPhone(user.getUserDetail().getPhone());
			old.getUserDetail().setEmail(user.getUserDetail().getEmail());
			old.getUserDetail().setBackground(user.getUserDetail().getBackground());
			old.getUserDetail().setId(user.getUserDetail().getId());
			old.getUserDetail().setImage(path);
			Transaction tran = null;
			try {
				tran = dao.getSession().beginTransaction();
				dao.update(old);
				tran.commit();
				request.put("tip", "信息已经上传，等待验证！");
				return true;
			} catch (Exception e) {
				if(tran != null) tran.rollback();
				return false;
			} finally {
				dao.getSession().close();
			}
		}else{
			request.put("tip", "你已经申请了家教资格,请勿重复操作！");   
			return true;
		}
		
	}
}
